<template>
  <div class="register-container">
    <el-form ref="form" :model="registerForm" label-width="80px" class="register-form">
      <h2>注册管理员账号</h2>

      <el-form-item label="用户名">
        <el-input v-model="registerForm.username"></el-input>
      </el-form-item>

      <el-form-item label="密码">
        <el-input v-model="registerForm.password" show-password></el-input>
      </el-form-item>

      <el-form-item label="确认密码">
        <el-input v-model="registerForm.confirmPassword" show-password></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">注册</el-button>
        <el-button @click="$router.push('/login')" type="text">已有账号？去登录</el-button>
      </el-form-item>

      <p style="color: #999; font-size: 13px;">提示：注册成功后可使用该账号登录系统</p>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      }
    }
  },
  methods: {
    onSubmit() {
      const { username, password, confirmPassword } = this.registerForm

      if (!username || !password || !confirmPassword) {
        this.$message.error('所有字段均为必填项')
        return
      }

      if (password !== confirmPassword) {
        this.$message.error('两次输入的密码不一致')
        return
      }

      const existingUser = localStorage.getItem(`user_${username}`)
      if (existingUser) {
        this.$message.error('该用户名已被注册')
        return
      }

      localStorage.setItem(`user_${username}`, JSON.stringify({ username, password }))
      this.$message.success('注册成功，请登录')
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.register-form {
  width: 400px;
  padding: 30px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}
</style>